package com.yuerlive.rainbow.module.main.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.common.BaseRecycleViewAdapter;
import com.yuerlive.rainbow.entity.LiveInfo;
import com.yuerlive.rainbow.entity.ObjectWrapper;
import com.yuerlive.rainbow.utils.GlideUtil;
import com.yuerlive.rainbow.utils.L;
import com.yuerlive.rainbow.widget.CircleImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by xiaoyi on 2017/5/26.
 */

public class MainFollowedAdapter extends BaseRecycleViewAdapter {

    private List<ObjectWrapper> mDatas;
    private final int TYPE_ITEM = 0;
    public  static final int TYPE_HEADER=1;
    private static final int TYPE_EMPTY = 2;
    private static final String TAG="MainFollowedAdapter";

    public MainFollowedAdapter(Context mContext, List<ObjectWrapper> datas) {
        super(mContext);
        this.mDatas = datas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        L.e(TAG,"onCreateViewHolder::"+viewType);
        switch (viewType) {
            case TYPE_EMPTY:
                holder=new EmptyHolder(mInflater.inflate(R.layout.layout_exception,parent,false));
                break;
            case TYPE_HEADER:
                holder=new MainFollowedAdapter.HeaderHolder(mInflater.inflate(R.layout.layout_header,parent,false)) ;
                break;
            case TYPE_ITEM:
                holder = new LiveHolder(mInflater.inflate(R.layout.layout_livecard, parent, false));
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE_ITEM:
                initLiveData((LiveHolder) holder, position);
                break;
        }
    }
    @Override
    public int getItemViewType(int position) {
        if(mDatas.size()==1){
            return TYPE_EMPTY;
        }
        return mDatas.get(position).type;
    }
    @Override
    public int getItemCount() {
        if (mDatas == null) return 0;
        return mDatas.size();
    }
    static  class  EmptyHolder extends RecyclerView.ViewHolder{
        public EmptyHolder(View itemView){
            super(itemView);
        }
    }
    class LiveHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.imgHeader)
        CircleImageView imgHeader;
        @BindView(R.id.tvNickName)
        TextView tvNickName;
        @BindView(R.id.tvHot)
        TextView tvHot;
        @BindView(R.id.imgConver)
        ImageView imgConver;
        @BindView(R.id.imgState)
        ImageView imgState;
        @BindView(R.id.imgGame)
        ImageView imgGame;

        public LiveHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(v -> {
                if (mItemClickListener != null)
                    mItemClickListener.onItemClick(v, getLayoutPosition());
            });

        }
    }
    static class HeaderHolder extends  RecyclerView.ViewHolder{
        public HeaderHolder(View itemView){
            super(itemView);
        }
    }
    private void initLiveData(LiveHolder holder, int position) {
        LiveInfo liveData = (LiveInfo) mDatas.get(position).object;
        holder.tvHot.setText("" + liveData.getOnlineNum());
        holder.tvNickName.setText(liveData.getNickname());
        GlideUtil.load(mContext, liveData.getUserIcon(), holder.imgHeader);
        GlideUtil.load(mContext, liveData.getLiveIcon(), holder.imgConver);
        if(liveData.getGameId()==1){
            holder.imgGame.setImageResource(R.drawable.game_niuniu);
        }else if (liveData.getGameId() == 2){
            holder.imgGame.setImageResource(R.drawable.game_cat);
        }
        holder.imgState.setVisibility(liveData.getState()==1?View.VISIBLE:View.VISIBLE);
        int state = liveData.getState();
        if (state == 0){
            holder.imgState.setImageResource(R.drawable.state_liveoff);
        }else if (state == 1 || state==3){
            holder.imgState.setImageResource(R.drawable.state_living);
        }else if (state == 2){
            holder.imgState.setImageResource(R.drawable.state_game);
        }
    }


}
